package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.workflow.domain.ActDynamicForm;
import com.ruoyi.workflow.domain.bo.ActDynamicFormBo;
import com.ruoyi.workflow.domain.vo.ActDynamicFormVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.EmployeeInfoBo;
import com.ruoyi.oa.domain.vo.EmployeeInfoVo;
import com.ruoyi.oa.domain.EmployeeInfo;
import com.ruoyi.oa.mapper.EmployeeInfoMapper;
import com.ruoyi.oa.service.IEmployeeInfoService;
import com.ruoyi.workflow.service.IProcessInstanceService;
import com.ruoyi.workflow.service.IUserService;
import com.ruoyi.workflow.utils.WorkFlowUtils;
import java.util.stream.Collectors;
import cn.hutool.core.util.ObjectUtil;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 员工信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-21
 */
@RequiredArgsConstructor
@Service
public class EmployeeInfoServiceImpl implements IEmployeeInfoService {

    private final EmployeeInfoMapper baseMapper;

    private final IProcessInstanceService iProcessInstanceService;

    private final IUserService iUserService;
    /**
     * 查询员工信息
     */
    @Override
    public EmployeeInfoVo queryById(String id){
        EmployeeInfoVo vo= baseMapper.selectVoById(id);
        return vo;
    }

    /**
     * 查询员工信息列表
     */
    @Override
    public TableDataInfo<EmployeeInfoVo> queryPageList(EmployeeInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmployeeInfo> lqw = buildQueryWrapper(bo);
        Page<EmployeeInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工信息列表
     */
    @Override
    public List<EmployeeInfoVo> queryList(EmployeeInfoBo bo) {
        LambdaQueryWrapper<EmployeeInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmployeeInfo> buildQueryWrapper(EmployeeInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmployeeInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()),EmployeeInfo::getName,bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getSex()),EmployeeInfo::getSex,bo.getSex());
        return lqw;
    }

    /**
     * 新增员工信息
     */
    @Override
    public EmployeeInfoBo insertByBo(EmployeeInfoBo bo) {
        EmployeeInfo add = BeanUtil.toBean(bo, EmployeeInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return bo;
    }

    /**
     * 修改员工信息
     */
    @Override
    public EmployeeInfo updateByBo(EmployeeInfoBo bo) {
        EmployeeInfo update = BeanUtil.toBean(bo, EmployeeInfo.class);
        validEntityBeforeSave(update);
        int i =baseMapper.updateById(update);
        return update;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmployeeInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除员工信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (String id : ids) {
            String processInstanceId = iProcessInstanceService.getProcessInstanceId(id);
            if (StringUtils.isNotBlank(processInstanceId)) {
                iProcessInstanceService.deleteRuntimeProcessAndHisInst(processInstanceId);
            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
